package com.zsck.commonbase.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zsck.commonbase.R;
import com.zsck.commonbase.util.OnDelayClickListener;

/**
 * 创建者     Bowllboy
 * 创建时间   2020/4/24 11:04
 * 描述	      ${TODO}
 */
public class SearchView extends LinearLayout {

    private LinearLayout mLl_select;
    private TextView mTv_flag;
    private EditText mEt_input;
    private ImageView ivDelete;
    public SearchView(Context context) {
        this(context, null);
    }

    public SearchView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SearchView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    private void initView(final Context context, AttributeSet attrs) {
        LinearLayout view = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.search_editor_view, this, true);
        mLl_select = view.findViewById(R.id.ll_select);
        mTv_flag = view.findViewById(R.id.tv_flag);
        mEt_input = view.findViewById(R.id.et_input);
        ivDelete= view.findViewById(R.id.iv_delete);
        TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.SearchView);
        String hintText = attributes.getString(R.styleable.SearchView_hint_text);
        mEt_input.setHint(hintText);
        mEt_input.setFocusable(false);
        attributes.recycle();
        mEt_input.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!TextUtils.isEmpty(mEt_input.getText().toString().trim())) {
                    ivDelete.setVisibility(VISIBLE);
                } else {
                    ivDelete.setVisibility(INVISIBLE);
                }
                /*if(mOnSelectClickListener != null){
                    mOnSelectClickListener.onTextChange(mEt_input);
                }*/
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        mLl_select.setOnClickListener(new OnDelayClickListener() {
            @Override
            protected void onDelayClick(View v) {
                if (mOnSelectClickListener != null) {
                    mOnSelectClickListener.selectClick(mTv_flag);
                }
            }
        });
        mEt_input.setOnClickListener(new OnDelayClickListener() {
            @Override
            protected void onDelayClick(View v) {
                if (mOnSelectClickListener != null) {
                    mOnSelectClickListener.searchClick(mEt_input);
                }
            }
        });
        mEt_input.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId== EditorInfo.IME_ACTION_SEARCH) {
                    if (mOnSelectClickListener != null) {
                        mOnSelectClickListener.dosearchAction(mEt_input);
                    }
                }
                return false;
            }
        });
        ivDelete.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                mEt_input.setText("");
                ivDelete.setVisibility(INVISIBLE);
                if(mOnSelectClickListener != null){
                    mOnSelectClickListener.doDelteAll(mEt_input);
                }
            }
        });
    }

    public void setOnClickListener(onClickListener l) {
        if (l != null) {
            mOnSelectClickListener = l;
        }

    }

    private onClickListener mOnSelectClickListener;

    public interface onClickListener {
        void selectClick(TextView textView);
        void searchClick(EditText editText);
        void dosearchAction(EditText editText);
        void doDelteAll(EditText editText);
    }
public void focusable(boolean b){
    mEt_input.setFocusable(b);
    mEt_input.setFocusableInTouchMode(b);
    if (b) {
        mEt_input.requestFocus();
        mEt_input.findFocus();
    } else {
        mEt_input.clearFocus();
    }
}
    public void setFlagText(String flagText) {
        mTv_flag.setText(flagText);
    }
}
